import java.util.Arrays;

public class MyStack {
    public Character [] elem;
    public int usesize;

    public MyStack() {
        this.elem = new Character[2];
    }
    public boolean isValid(String s) {
       for(int i = 0; i < s.length();i++){
           char ch = s.charAt(i);
           if(isfull()){
               grow();
           }
           if(ch == '[' || ch == '{' || ch == '('){
               push(ch);
           }
           if(ch == ']'|| ch == '}'|| ch == ')'){
               if(isfull()){
                   return false;
               }else {
                   if(pop() != ch){
                       return false;
                   }
               }
           }
       }
       if(!empty()){
           return false;
       }
       return true;
    }
    public void push(char data){
      if(empty()){
          grow();
      }
      elem[usesize] = data;
      usesize++;
    }
    public void grow(){
        this.elem = Arrays.copyOf(elem,2*elem.length);
    }
    public Character pop(){
        if(isfull()){
            return null;
        }
        Character val = elem[usesize-1];
        usesize--;
        return val;
    }
    public Character peek(){
     if(usesize == 0){
         return null;
     }
     return elem[usesize - 1];
    }
    public int size(){
      return usesize;
    }
    public boolean empty(){
        return usesize == elem.length;
    }
    public boolean isfull(){
        return usesize == 0;
    }

}
